package
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	
	import starling.core.Starling;
	
	[SWF(width="800", height="600", frameRate="60", backgroundColor="#000000")]
	public class StarlingMovieClip extends Sprite
	{
		private const loader:Loader = new Loader();
		
		private var _starling:Starling;
		
		public function StarlingMovieClip()
		{
			super();
		
			addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
		}
		
		private function addedToStage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			
			loader.load(new URLRequest("../test_rect.swf"));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
		}
		
		private function loadComplete(event:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
			
			StarlingCanvas.mc = getMovieClip();
			
			_starling = new Starling(StarlingCanvas, stage);
			_starling.enableErrorChecking = true;
			_starling.start();		
		}
		
		private function getMovieClip():MovieClip
		{
			const app:ApplicationDomain = getLoadedSWFAppDomain();
			if (app && app.hasDefinition("Rect"))
			{
				return new (Class(app.getDefinition("Rect")));
			}
			return null;
		}
		
		private function getLoadedSWFAppDomain():ApplicationDomain
		{
			return loader ? loader.contentLoaderInfo.applicationDomain : null;
		}
	}
}